﻿using Aspose.Cells.Drawing;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Model.Result;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Util;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

/***************************************************************************************************
 *
 * 更新人：sitong.dong
 * 描述：库位管理
 * 修改时间：2022.06.22
 * 修改日志：系统迭代升级
 *
 **************************************************************************************************/
namespace Estsh.Core.Wms.Services
{
    public class PurchaseManageService : BaseService<WmsPurchase>, IPurchaseManageService
    {
        private readonly IPurchaseManageRepository repository;
        public PurchaseManageService(IPurchaseManageRepository _repository) : base(_repository)
        {
            repository = _repository;
        }

        public Hashtable getPurchaseListByPage(string orderNo, string orderType,string orderStatus, string startTime, string endTime, string enabled, int factoryId, Pager pager, String direction, String sort)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no like '%" + orderNo.Trim() + "%' ";
            }

            if (orderType != null && !orderType.Trim().Equals(""))
            {
                strWhere += "and a.order_type ='" + orderType.Trim() + "' ";
            }

            if (orderStatus != null && !orderStatus.Trim().Equals(""))
            {
                strWhere += "and a.order_status ='" + orderStatus.Trim() + "' ";
            }

            if (!String.IsNullOrEmpty(startTime) && !String.IsNullOrEmpty(endTime))
            {
                strWhere += " and  a.create_time BETWEEN '" + startTime.Trim() + "' AND '" + endTime.Trim() + "'";
            }
            else if (String.IsNullOrEmpty(startTime) && !String.IsNullOrEmpty(endTime))
            {
                strWhere += " and  a.create_time < '" + endTime.Trim() + "'";
            }
            else if (!String.IsNullOrEmpty(startTime) && String.IsNullOrEmpty(endTime))
            {
                strWhere += " and  a.create_time >'" + startTime.Trim() + "'";
            }

            if (enabled != null && !enabled.Trim().Equals(""))
            {
                strWhere += "and a.enabled = '" + enabled.Trim() + "' ";
            }
            strWhere += " and a.factory_id = " + factoryId + " ";
            String orderBy = "";
            if (sort != null && !"".Equals(sort.Trim()))
            {
                orderBy += typeof(WmsPurchase).GetEntityColumnName(sort.Trim()) + " " + direction;
            }
            else
            {
                orderBy += typeof(WmsPurchase).GetEntityColumnName("orderNo") + " " + direction;
            }

            return repository.getPurchaseListByPage(pager.pageSize, pager.pageNo, strWhere, orderBy);

        }

        public Hashtable getPurchaseDetailListByPage(string orderNo, string enabled, int factoryId)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.order_no like '%" + orderNo.Trim() + "%' ";
            }

            if (enabled != null && !enabled.Trim().Equals(""))
            {
                strWhere += "and a.enabled = '" + enabled.Trim() + "' ";
            }
            strWhere += " and a.factory_id = " + factoryId + " ";


            return repository.getPurchaseDetailListByPage(strWhere);
        }

        public Hashtable getStockListByPage(string orderNo, string partNo, string enabled, int factoryId)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.ref_order_no like '%" + orderNo.Trim() + "%'";
            }
            if (partNo != null && !partNo.Trim().Equals(""))
            {
                strWhere += "and a.part_no = '" + partNo.Trim() + "'";
            }
            if (enabled != null && !enabled.Trim().Equals(""))
            {
                strWhere += "and a.enabled = '" + enabled.Trim() + "'";
            }
            strWhere += " and a.factory_id = " + factoryId + " ";

            return repository.getStockListByPage(strWhere);
        }

        public List<SysStock> getStockListByPrint(string orderNo,  string enabled, int factoryId)
        {
            Hashtable result = new Hashtable();

            String strWhere = " 1=1 ";
            if (orderNo != null && !orderNo.Trim().Equals(""))
            {
                strWhere += "and a.ref_order_no like '%" + orderNo.Trim() + "%'";
            }
            if (enabled != null && !enabled.Trim().Equals(""))
            {
                strWhere += "and a.enabled = '" + enabled.Trim() + "'";
            }
            strWhere += " and a.factory_id = " + factoryId + " ";

            return repository.getStockListByPrint(strWhere);
        }

        public List<SysPart> GetPartInfo(string part_no)
        {
            return repository.GetPartInfo(part_no);
        }
        public List<KeyValueResult> GetErpwarehouse()
        {
            return repository.GetErpwarehouse();
        }
        public List<KeyValueResult> GetOrderStatus()
        {
            return repository.GetOrderStatus();
        }
        public List<KeyValueResult> getSelectFactory()
        {
            return repository.getSelectFactory();
        }

        public List<KeyValueResult> getSelectWarehouse()
        {
            return repository.getSelectWarehouse();
        }
        public List<SysWarehouse> getSelectWarehouse(string warehouseid)
        {
            return repository.getSelectWarehouse(warehouseid);
        }
        public List<SysZone> getSelectZone(string zoneid)
        {
            return repository.getSelectZone(zoneid);
        }
        public List<KeyValueResult> getSelectZone()
        {
            return repository.getSelectZone();
        }
        public List<KeyValueResult> getSelectVendor()
        {
            return repository.getSelectVendor();
        }

        public List<SysVendor> getSelectVendor(string vendorName)
        {
            return repository.getSelectVendor(vendorName);
        }
        public List<KeyValueResult> GetPart()
        {
            return repository.GetPart(0);
        }

        public List<SysPart> GetPart(string PartNo)
        {
            return repository.GetPart(0, PartNo);
        }

        public List<KeyValueResult> GetOrderType()
        {
            return repository.GetOrderType();
        }

        /// <summary>
        /// 保存菜单数据
        /// </summary>
        /// <param name="htParams"></param>
        /// <returns></returns>
        public int savePurchaseManage(WmsPurchase htParams, IList<WmsPurchaseDetail> htDetailParams)
        {
            return repository.savePurchaseManage(htParams, htDetailParams);
        }
        public List<SysPart> GetPartNoInfo(string part_no)
        {
            return this.repository.GetPartNoInfo(part_no);
        }
        public List<SysPart> GetPartNoInfoByPartNo(string part_no)
        {
            return this.repository.GetPartNoInfoByPartNo(part_no);
        }
        public List<SysPart> GetPartSpecInfo(string partSpec)
        {
            return this.repository.GetPartSpecInfo(partSpec);
        }
        public List<SysPart> GetPartSpecInfoByPartSpec(string partSpec)
        {
            return this.repository.GetPartSpecInfoByPartSpec(partSpec);
        }
        /// <summary>
        /// 查看菜单详情
        /// </summary>
        /// <param name="ruid"></param>
        /// <returns></returns>
        public Hashtable getWarehouseDetail(String warehouse_id)
        {
            warehouse_id = " warehouse_id = " + warehouse_id;
            List<SysWarehouse> dt = repository.getList(warehouse_id, "");
            Hashtable result = new Hashtable();
            result.Add("warehouseId", dt[0].WarehouseId);
            result.Add("factoryId", dt[0].FactoryId);
            result.Add("factoryName", dt[0].FactoryName);
            result.Add("warehouseName", dt[0].WarehouseName);
            result.Add("warehouseDesc", dt[0].WarehouseDesc);
            result.Add("enabled", dt[0].Enabled);
            return result;
        }

        public Hashtable onBarcodeGenerator(string orderNo, string userID, string factoryID, string factoryCode)
        {
            orderNo = orderNo.Substring(0, orderNo.Length - 1);
            return this.repository.onBarcodeGenerator(orderNo, userID, factoryID, factoryCode);
        }
        /// <summary>
        /// 关闭
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int onClose(String ids)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.onClose(ids);
        }

        /// <summary>
        /// 启用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int EnableData(String ids)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.EnableData(ids);
        }

        /// <summary>
        /// 禁用
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DisableData(String ids)
        {
            ids = ids.Substring(0, ids.Length - 1);
            return this.repository.DisableData(ids);
        }
    }
}